package com.example.fastdfsdemo.service.impl;

import com.example.fastdfsdemo.entity.FastDFSFile;
import com.example.fastdfsdemo.service.FastDFSService;
import com.example.fastdfsdemo.util.DateAndStringExchanger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 文件路径上传和获取的实现类
 * @author 276834
 */
@Slf4j
@Service
public class FastDFSServiceImpl implements FastDFSService {

    /**
     * 使用JdbcTemplate进行数据库访问
     */
    @Autowired
    private JdbcTemplate template;


    @Override
    public int saveUploadFilePath(FastDFSFile file) {
        String sql
                = "insert into fdfs_file\n" +
                "(user_account, file_name, file_extension, file_path, upload_time)\n" +
                "values\n" +
                "('" + file.getUser_account() +
                "', '" + file.getFile_name() +
                "', '" + file.getFile_extension() +
                "', '" + file.getFile_path() +
                "', '" + DateAndStringExchanger.getDatetimeFormatString(file.getUpload_time()) + "')";
        try {
            template.execute(sql);
        } catch (DataAccessException e) {
            e.printStackTrace();
            log.error("数据插入执行失败！");
            return 0;
        }
        return 1;
    }

    @Override
    public List<String> getUploadFilePath(String user_account, String file_name) {
        String sql
                = "select file_path " +
                "from fdfs_file t " +
                "where t.user_account = '" + user_account + "' " +
                "and t.file_name = '" + file_name + "'";
        List<String> result = template.queryForList(sql, String.class);
        return result;
    }

    @Override
    public List<String> getUploadFilePath(String user_account) {
        String sql
                = "select file_path " +
                "from fdfs_file t " +
                "where t.user_account = '" + user_account + "' ";

//        RowMapper<String> rowMapper = new RowMapper<String>() {
//            @Override
//            public String mapRow(ResultSet resultSet, int i) throws SQLException {
//                return resultSet.getString(i);
//            }
//        };
        List<String> result = template.queryForList(sql, String.class);
        return result;
    }

    @Override
    public List<FastDFSFile> getUploadFilePath() {
        String sql
                = "select id, user_account, file_name, file_extension, file_path, upload_time " +
                "from fdfs_file " +
                "where 1=1";
        List<Map<String, Object>> result = template.queryForList(sql);
        List<FastDFSFile> fastDFSFiles = new ArrayList<>();

        for(int i = 0; i < result.size(); i++){
            FastDFSFile fastDFSFile = new FastDFSFile();
            fastDFSFile.setId(((Integer) result.get(i).get("id")).toString());
            fastDFSFile.setUser_account((String) result.get(i).get("user_account"));
            fastDFSFile.setFile_name((String) result.get(i).get("file_name"));
            fastDFSFile.setFile_extension((String) result.get(i).get("file_extension"));
            fastDFSFile.setFile_path((String) result.get(i).get("file_path"));
            fastDFSFile.setUpload_time(DateAndStringExchanger.dateToLocalDateTime((LocalDateTime) result.get(i).get("upload_time")));
            //提供下载别名参数
            fastDFSFile.setFile_path(fastDFSFile.getFile_path() + "?attname=" + fastDFSFile.getFile_name());
            fastDFSFiles.add(fastDFSFile);
        }

        return fastDFSFiles;
    }
}
